package com.jwsoft.manager.core.dao.service.impl;

import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.module.base.common.vo.dict.DictQueryVO;
import com.bifang.module.base.core.integration.DictIntegration;
import com.jwsoft.manager.common.enums.JhSyncImportEnum;
import com.jwsoft.manager.core.dao.model.EduSyncJhImportBatch;
import com.jwsoft.manager.core.dao.mapper.EduSyncJhImportBatchMapper;
import com.jwsoft.manager.core.dao.service.EduSyncJhImportBatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwsoft.manager.core.util.EduHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 金华市教育同步导入批次表服务实现类
 *
 * @author zhiyuan
 * @since 2024-04-11
 */
@Service
public class EduSyncJhImportBatchServiceImpl extends ServiceImpl<EduSyncJhImportBatchMapper, EduSyncJhImportBatch> implements EduSyncJhImportBatchService {

    @Autowired
    private EduHelper eduHelper;

    @Autowired
    private DictIntegration dictIntegration;
    @Override
    public Long createImportBatch(JhSyncImportEnum importType) {
        Integer year = eduHelper.thisYear();
        String areaCode = SessionUtil.getAreaCode();
        EduSyncJhImportBatch syncJhImport =  new EduSyncJhImportBatch();
        syncJhImport.setYear(year);
        syncJhImport.setImportType(importType.getType());
        syncJhImport.setAreaCode(areaCode);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String newDate = now.format(formatter);
        DictQueryVO dictQueryVO = new DictQueryVO();
        dictQueryVO.setDictField("AREA");
        dictQueryVO.setDictKey(areaCode);
        String areaName = dictIntegration.getValue(dictQueryVO);
        syncJhImport.setImportBatchName(areaName + newDate + importType.getName());
        save(syncJhImport);
        return syncJhImport.getImportBatchId();
    }
}
